package com.feel.mall.userservice.controller;

import com.feel.mall.common.bean.Addr;
import com.feel.mall.common.bean.RespBean;
import com.feel.mall.common.exception.FeelException;
import com.feel.mall.common.service.AddrService;
import com.feel.mall.common.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: 刘永钦
 * @Description:
 */
@RestController
@RequestMapping("/addr")
public class AddrController {
    @Autowired
    private AddrService addrService;

    /**
     * 添加地址
     */
    @PostMapping
    public RespBean insert(@RequestBody Addr addr,@RequestHeader("token") String token) {
        // 解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        addr.setUserId(userId);
        addrService.insert(addr);
        return RespBean.ok("添加成功");
    }
    /**
     * 删除地址
     */
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") Integer id, @RequestHeader("token") String token) throws FeelException {
        // 解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        addrService.delete(id, userId);

        return RespBean.ok("删除成功");
    }

    /**
     * 修改地址
     */
    @PutMapping
    public RespBean update(@RequestBody Addr addr,  @RequestHeader("token") String token) throws FeelException {
        // 解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        addrService.update(addr, userId);

        return RespBean.ok("修改成功");
    }
    /**
     * 查询当前用户的所有地址信息
     */
    @GetMapping
    public RespBean getByUser(@RequestHeader("token") String token) {
        // 解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        Addr addr = new Addr();
        addr.setUserId(userId);

        List<Addr> list = addrService.getByCondition(addr);
        return RespBean.ok("查询成功", list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public RespBean getById(@PathVariable("id") Integer id, @RequestHeader("token") String token) throws FeelException {
        // 解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        Addr addr = addrService.getById(id, userId);

        return RespBean.ok("查询成功" , addr);
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/default/{id}")
    public RespBean setDefaultAddr(@PathVariable("id") Integer id, @RequestHeader("token") String token) throws FeelException {
        // 解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        addrService.setDefaultAddr(id,userId);

        return RespBean.ok("设置成功");
    }

}
